<template>
  <div class="login-container">
    <div class="loginShow">
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form">
        <div class="form-container register">
          <p class="title">忘记密码</p>
          <p class="summary">物流大掌柜</p>
          <el-form-item prop="mobile">
            <span class="item-name">手机号</span>
            <el-input v-model="registerForm.mobile" type="text" auto-complete="off" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <span class="item-name">验证码</span>
            <el-input
              v-model="registerForm.code"
              auto-complete="off"
              placeholder="验证码"
              @keyup.enter.native="handleRegister"
            >
            </el-input>
            <div class="login-code">
              <el-button type="text" class="login-code-img" @click="handleGetPhoneCode">获取验证码</el-button>
            </div>
          </el-form-item>
          <el-form-item prop="pwd">
            <span class="item-name">密码</span>
            <el-input
              v-model="registerForm.pwd"
              type="pwd"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleRegister"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="rpwd">
            <span class="item-name">确认密码</span>
            <el-input
              v-model="registerForm.rpwd"
              type="rpwd"
              auto-complete="off"
              placeholder="确认密码"
              @keyup.enter.native="handleRegister"
            >
            </el-input>
          </el-form-item>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleRegister"
          >
            <span v-if="!loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div class="remember-foot">
            <router-link class="link-type" :to="'/login'">登录</router-link>
            <router-link class="link-type" :to="'/register'">注册</router-link>
          </div>
        </div>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { forgetPassword, getPhoneCode } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.pwd !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        mobile: "",
        pwd: "",
        rpwd: "",
        code: "",
      },
      registerRules: {
        mobile: [
          { required: true, trigger: "blur", message: "请输入您的手机号" },
          { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ],
        pwd: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        rpwd: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    
  },
  methods: {
    handleGetPhoneCode() {
      getPhoneCode({ mobile: this.registerForm.mobile, reqType: 'register' }).then(res => {
        // this.$modal.alertSuccess("验证码已发送至您的手机，请注意查收");214585
        if(res && res.code === 0) {
          this.$modal.alertSuccess(res.data);
        }
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          forgetPassword(this.registerForm).then(res => {
            const mobile = this.registerForm.mobile;
            this.$alert("<font color='red'>恭喜你，您的账号 " + mobile + " 重置成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
